package com.inhim.pjhim.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.inhim.pjhim.R;

public class CountryCodeDialog extends Dialog {
    private View tvCancel;
    private View tvHongKong;
    private View tvChina;

    public CountryCodeDialog(@NonNull Context context) {
        this(context, 0);
    }

    public CountryCodeDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    private void init() {
        View rootView = View.inflate(getContext(), R.layout.dialog_country_code, null);
        tvCancel = rootView.findViewById(R.id.tvCancel);
        tvHongKong = rootView.findViewById(R.id.tvHongKong);
        tvChina = rootView.findViewById(R.id.tvChina);

        setContentView(rootView);

        //获取当前Activity所在的窗体
        Window dialogWindow = this.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0); //消除边距

        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.y = 0;//设置Dialog距离底部的距离
//       将属性设置给窗体
        dialogWindow.setAttributes(lp);

        tvCancel.setOnClickListener(v -> {
            cancel();
        });
        tvChina.setOnClickListener(v -> {
            if (listenner != null) {
                listenner.onCodeSelected("+86");
            }
            dismiss();
        });
        tvHongKong.setOnClickListener(v -> {
            if (listenner != null) {
                listenner.onCodeSelected("+852");
            }
            dismiss();
        });
    }

    private Listenner listenner;

    public void setListenner(Listenner listenner) {
        this.listenner = listenner;
    }

    public interface Listenner {
        void onCodeSelected(String code);
    }

}
